#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QScrollBar>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>


//QString getChineseSpell(QString& src);
QString FirstLetter(int nCode);
char convert(int n);

bool LessThan(const QString &s1, const QString &s2)
{
    return (s1.localeAwareCompare(s2) < 0);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString str = tr("我叫韩若熙我叫韩若熙");
    QStringList strList;
    strList<<"我叫韩若熙我叫韩若熙"<<"我叫韩若熙我叫韩若熙";
    for(int i =0;i <strList.count();i++)
    {
        QString strName = strList.at(i);

        QString character = getChineseSpell(strName);

        //qDebug()<<character.toUtf8().data();
    }


    QString character = getChineseSpell(str);
    //qDebug()<<character.toUtf8().data();

    /*QString r;
    int length = character.length();
    for(int i=0;i<length;i++)
    {
        if (!r.contains(character[i]))
        {
            r += character[i];
        }
    }
    qDebug()<<r;*/


    /*QStringList stringList;
    stringList<<"我"<<"叫"<<"韩"<<"若"<<"熙"<<"哈"<<"楼"<<"我"<<"叫"<<"韩"<<"若"<<"熙";
    std::sort(stringList.begin(), stringList.end(), LessThan);

    for(int i =0;i<stringList.count();i++)
    {
        //QString str = stringList.at(i);
        qDebug()<<stringList.at(i).toUtf8().data();
    }

    //qDebug()<<r;*/



   readJson();
   init();
}

void MainWindow::readJson()
{
    QFile file("../build-untitled-Desktop_Qt_5_5_0_MinGW_32bit-Debug/debug/config.json");
    QByteArray bArrJson;
    if( file.open(QIODevice::ReadOnly) )
    {
        //qDebug()<<"open suc!";
        bArrJson = file.readAll();
        file.close();
    }
    //qDebug()<<bArrJson;
    QTextCodec* codec = QTextCodec::codecForName("GB18030");
    QString str = codec->toUnicode(bArrJson);

    QJsonDocument jsDoc;
    QJsonParseError jsError;
    jsDoc = QJsonDocument::fromJson(str.toUtf8(), &jsError);
    if( jsError.error != QJsonParseError::NoError )
    {
        qDebug()<<"JSON文件格式错误...";
        return;
    }

    QJsonObject companyObj = jsDoc.object();
    QJsonValue value = companyObj.value(QString("company"));
    QJsonObject item = value.toObject();
    count = item.value("count").toInt();
    QJsonArray bArry = item.value("data").toArray();
    //qDebug()<<count<<bArry.size();


    QString firstString;//所有首汉字首拼音字母

    for(int i=0; i<bArry.size();i++)
    {
        QJsonObject obj = bArry.at(i).toObject();

        for(int j =0;j<count;j++)
        {
            QString index = QString("name_%1").arg(j+1);

            if(obj.contains(index))
            {
                QJsonValue name_value = obj.take(index);
                if(name_value.isString())
                {
                    QString name = name_value.toString();
                    //qDebug()<<name.toUtf8().data();

                    //QString strzw = QString::fromLocal8Bit(name.toUtf8().data());

                    QString character = getChineseSpell(name);
                    firstString.append(character);
                    if(character == "A")
                        count_A++;
                    if(character == "B")
                        count_B++;
                    if(character == "C")
                        count_C++;
                    //qDebug()<<character;
                    //m_map.insert(character,name);

                    companyNameList.append(name);
                }
            }
        }
    }

    //qDebug()<<companyNameList;

//    qDebug()<<character.toUtf8().data();

    qDebug()<<firstString;

    int length = firstString.length();
    for(int i=0;i<length;i++)
    {
        if (!lastString.contains(firstString[i]))
        {
            lastString += firstString[i];
        }
    }
    qDebug()<<lastString;

    std::sort(companyNameList.begin(), companyNameList.end(), LessThan);

    for(int i = 0;i<companyNameList.count();i++)
    {
        qDebug()<<companyNameList.at(i).toUtf8().data();
    }
}

static int nCurScroller=0; //翻页时的当时滑动条位置
static int pageValue = 3; // 一页显示条数

void MainWindow::init()
{
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setRowCount(count);

    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //ui->tableWidget->setShowGrid(false);

    int j = 0;
    int c = 0;

    qDebug()<<count_A<<count_B<<count_C;

    for(int i =0;i<count;i++)
    {
        //QString indexName = QString("Jan%1").arg(i);
//        if( 1<10)
//        {
//           ui->tableWidget->setItem(i,0,new QTableWidgetItem(indexName));
//        }


//        if(i >=10)
//        {
//           ui->tableWidget->setItem(i-100,1,new QTableWidgetItem(indexName));
//        }
        QString string = companyNameList.at(i);
        QString character = getChineseSpell(string);


        if(character == "A")
        {
            ui->tableWidget->setItem(0,0,new QTableWidgetItem("A"));
            j++;
//            ui->tableWidget->setItem(j,0,new QTableWidgetItem(string));
//            qDebug()<<j;
//            c =i;
//            qDebug()<<count_A;

//            for(int j =0;j<count_A;j++)
//            {
                ui->tableWidget->setItem(j,0,new QTableWidgetItem(string));
//            }
        }

        if(character == "B")
        {
            ui->tableWidget->setItem(count_A+1,0,new QTableWidgetItem("B"));
            j++;
//            for(int j =0;j<count_B;j++)
//            {
                ui->tableWidget->setItem(j+1,0,new QTableWidgetItem(string));
//            }
        }
        if(character == "C")
        {
            ui->tableWidget->setItem(count_A+count_B+2,0,new QTableWidgetItem("C"));
            j++;
//            for(int j =0;j<count_B;j++)
//            {
                ui->tableWidget->setItem(j+1,0,new QTableWidgetItem(string));
//            }
        }

//        if(character == "C")
//        {
//            j++;
//            ui->tableWidget->setItem(c,0,new QTableWidgetItem("C"));

//            ui->tableWidget->setItem(j+1,0,new QTableWidgetItem(string));
//            c = j+1;
//        }
//        if(character == "D")
//        {

//            ui->tableWidget->setItem(c,0,new QTableWidgetItem("D"));
//            j++;
//            ui->tableWidget->setItem(j+1,0,new QTableWidgetItem(string));
//            c = j+1;
//        }
    }
//    ui->tableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
//    ui->tableWidget->setItem(1,0,new QTableWidgetItem("Feb"));
//    ui->tableWidget->setItem(2,0,new QTableWidgetItem("Mar"));

//    ui->tableWidget->setItem(0,1,new QTableWidgetItem("Jan's month"));
//    ui->tableWidget->setItem(1,1,new QTableWidgetItem("Feb's month"));
//    ui->tableWidget->setItem(2,1,new QTableWidgetItem("Mar's month"));
}

QString MainWindow::getChineseSpell(QString& src)
{
     wchar_t wchr;
     QString strPinYin;
     //获取所有汉字的首字母
     /*for (int i=0; i<src.length(); i++)
     {
        QString str = src.at(i);
        QByteArray arr = str.toLocal8Bit();
        wchr = (arr.at(0) & 0xff) << 8;
        wchr |= (arr.at(1) & 0xff);
        strPinYin.append(convert(wchr));
     }*/

     //获取首汉字的首字母
     QString str = src.at(0);
     QByteArray arr = str.toLocal8Bit();
     wchr = (arr.at(0) & 0xff) << 8;
     wchr |= (arr.at(1) & 0xff);
     strPinYin.append(convert(wchr));

     return strPinYin;
}

bool MainWindow::In(wchar_t start, wchar_t end, wchar_t code)
{
    if (code >= start && code <= end)
    {
        return true;
    }
    return false;
}

char MainWindow::convert(int n)
{
     if (In(0xB0A1,0xB0C4,n)) return 'A';
     if (In(0XB0C5,0XB2C0,n)) return 'B';
     if (In(0xB2C1,0xB4ED,n)) return 'C';
     if (In(0xB4EE,0xB6E9,n)) return 'D';
     if (In(0xB6EA,0xB7A1,n)) return 'E';
     if (In(0xB7A2,0xB8C0,n)) return 'F';
     if (In(0xB8C1,0xB9FD,n)) return 'G';
     if (In(0xB9FE,0xBBF6,n)) return 'H';
     if (In(0xBBF7,0xBFA5,n)) return 'J';
     if (In(0xBFA6,0xC0AB,n)) return 'K';
     if (In(0xC0AC,0xC2E7,n)) return 'L';
     if (In(0xC2E8,0xC4C2,n)) return 'M';
     if (In(0xC4C3,0xC5B5,n)) return 'N';
     if (In(0xC5B6,0xC5BD,n)) return 'O';
     if (In(0xC5BE,0xC6D9,n)) return 'P';
     if (In(0xC6DA,0xC8BA,n)) return 'Q';
     if (In(0xC8BB,0xC8F5,n)) return 'R';
     if (In(0xC8F6,0xCBF0,n)) return 'S';
     if (In(0xCBFA,0xCDD9,n)) return 'T';
     if (In(0xCDDA,0xCEF3,n)) return 'W';
     if (In(0xCEF4,0xD188,n)) return 'X';
     if (In(0xD1B9,0xD4D0,n)) return 'Y';
     if (In(0xD4D1,0xD7F9,n)) return 'Z';

     return '\0';
}

MainWindow::~MainWindow()
{
    delete ui;
}

//上一页
void MainWindow::on_pushButton_clicked()
{
    int maxValue = ui->tableWidget->verticalScrollBar()->maximum(); // 当前SCROLLER最大显示值25
    nCurScroller = ui->tableWidget->verticalScrollBar()->value();

    if(nCurScroller>0)
        ui->tableWidget->verticalScrollBar()->setSliderPosition(nCurScroller-pageValue);
    else
        ui->tableWidget->verticalScrollBar()->setSliderPosition(maxValue);
}

//下一页
void MainWindow::on_pushButton_2_clicked()
{
    int maxValue = ui->tableWidget->verticalScrollBar()->maximum(); // 当前SCROLLER最大显示值25
    nCurScroller = ui->tableWidget->verticalScrollBar()->value(); //获得当前scroller值

    if(nCurScroller<maxValue)
        ui->tableWidget->verticalScrollBar()->setSliderPosition(pageValue+nCurScroller);
    else
        ui->tableWidget->verticalScrollBar()->setSliderPosition(0);
}
